<?php
namespace Hilaoyu\LaravelExtend\Services\Validator\Redefinition;

use Hilaoyu\Utils\UtilRegex;
use Illuminate\Validation\Validator as BaseValidator;

/**
 * Created by PhpStorm.
 * User: laoyu
 * Date: 2018/2/14
 * Time: 上午2:04
 *
 * 扩展验证规则
 *
 * 可参考文档
 * http://laravelacademy.org/post/240.html
 *
 * illuminate/Validation
 * composer安装：
 * composer illuminate/Validation
 *
 * 依赖：illuminate/database
 */

class Validator extends BaseValidator
{
//多字段唯一验证
    public function validateUniqueMultipleOr($attribute, $value, $parameters){
        return $this->validateUniqueMultiple($attribute, $value, $parameters,'or');
    }
    public function validateUniqueMultipleAnd($attribute, $value, $parameters){
        return $this->validateUniqueMultiple($attribute, $value, $parameters,'and');
    }

    protected function validateUniqueMultiple($attribute, $value, $parameters,$type='and'){
        @list($table,$column,$except,$idColumn,$extraColumn,$extraValue) = $parameters;
        if(stripos($table,'.') !== false){
            @list($connection,$table) = explode('.',$table);
        }else{
            $connection = null;
        }




        $type = strtolower(trim($type));


        $query = \DB::connection($connection)->table($table);

        $column_arr = explode('~',$column);

        !$column_arr && $column_arr = [];

        if(!in_array($attribute,$column_arr)){
            $column_arr[] = $attribute;
        }
        if(!empty($column_arr)){
            $query->where(function ($query) use ($column_arr,$value,$type){
                //dd($query,$column_arr);
                foreach ($column_arr as $col){
                    if($type == 'or'){
                        $query->orWhere($col,'=',$value);
                    }else{
                        $query->where($col,'=',$value);
                    }

                }
            });

        }

        if(!empty($except)){
            empty($idColumn) && $idColumn = 'id';
            $query->where($idColumn,'<>',$except);
        }

        if(!empty($extraColumn)){
            $extraValue = (string)$extraValue;
            $query = $query->where($extraColumn,'=',$extraValue);
        }

        $count = $query->first();

        return empty($count);
    }

    public function validateUnique($attribute, $value, $parameters){
        //Util::dd($attribute, $value, $parameters);
        @list($table,$column,$except,$idColumn,$extraColumn,$extraValue,$extraColumn1,$extraValue1) = $parameters;
        if(stripos($table,'.') !== false){
            @list($connection,$table) = explode('.',$table);
            if(empty($connection)){
                $connection = null;
            }
        }else{
            $connection = null;
        }

        $query = \DB::connection($connection)->table($table);

        if(!$column){
            $column = $attribute;
        }



        $query = $query->where($column,'=',$value);

        if(!empty($except)){
            empty($idColumn) && $idColumn = 'id';
            $query = $query->where($idColumn,'<>',$except);
        }

        if(!empty($extraColumn)){
            //$extraValue = (string)$extraValue;
            $query = $query->where($extraColumn,'=',$extraValue);
        }
        if(!empty($extraColumn1)){
            //$extraValue = (string)$extraValue;
            $query = $query->where($extraColumn1,'=',$extraValue1);
        }



        $count = $query->first();

        return empty($count);
    }
    // 2024/08/26 10:27 无法带入where条件注释，用laravel默认规
    /*public function validateExists($attribute, $value, $parameters){
        @list($table,$column) = $parameters;
        if(stripos($table,'.') !== false){
            @list($connection,$table) = explode('.',$table);
            if(empty($connection)){
                $connection = null;
            }
        }else{
            $connection = null;
        }

        $query = \DB::connection($connection)->table($table);

        if(!$column){
            $column = $attribute;
        }
        $query->where($column,'=',$value);

        $E = $query->first();

        return !empty($E);
    }*/


    //手机号验证

    public function validateMobile($attribute, $value){
        return UtilRegex::isMobile($value);
    }

    //验证uuid
    public function validateUuid($attribute, $value){
        return UtilRegex::isUuid($value);
    }
    //验证账号
    public function validateAccount($attribute, $value){
        return UtilRegex::isAccount($value);
    }

    /**
     * 验证邮箱号
     * @param string $attribute
     * @param mixed $value
     * @return bool
     */
    public function validateEmail($attribute, $value, $parameters)
    {
        return UtilRegex::isEmail($value);
    }

    /**
     * 不少于8位 大写字母,小写字母,数字至少各一个
     * @param $attribute
     * @param $value
     * @return bool
     */
    public function validatePasswordStrong($attribute, $value){
        if (! is_string($value) && ! is_numeric($value)) {
            return false;
        }

//        return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0;
        return preg_match('/^.*(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*\d).*$/', $value) > 0;
    }

    public function validateNetworkPath($attribute, $value, $parameters)
    {
        if (is_array($value)){
            foreach ($value as $item){
                if (filter_var($value, FILTER_VALIDATE_URL) !== false) {
                    return true;
                }
            }
        } else {
            if (filter_var($value, FILTER_VALIDATE_URL) !== false) {
                return true;
            }
        }

        return false;
    }
    public function validateNotNetworkPath($attribute, $value, $parameters)
    {
        return !$this->validateNetworkPath($attribute, $value, $parameters);
    }

    public function validateAlphaNumber($attribute, $value){
    if (! is_string($value) && ! is_numeric($value)) {
        return false;
    }

    return preg_match('/^[a-zA-Z0-9]+$/u', $value) > 0;
}
}
